package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    //  获取容器中IRoleService的实现类对象，并注入Bean
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    //  查询方法
    @RequestMapping("/list")
    @RequiredPermission(name = "角色列表", expression = "role:list")
    public String list(Model model, QueryObject qo){
        //  调用Service层的query方法
        PageInfo<Role> pageInfo = roleService.query(qo);
        //  将获取到的数据设置到模型中
        model.addAttribute("pageResult", pageInfo);

        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions", permissions);

        //  找视图/WEB-INF/views/role/list.ftl
        return "role/list";
    }

    //  删除方法
    @RequestMapping("/delete")
    @RequiredPermission(name = "角色删除", expression = "role:delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            //  判断id是否存在
            if (id != null) {
                //  调用Service层的deleteById方法
                roleService.deleteById(id);
            }
            return new JsonResult(true, "删除成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult(false, "删除失败");
        }
    }

    //  去新增或修改页面方法
    @RequestMapping("/input")
    @RequiredPermission(name = "角色编辑", expression = "role:input")
    public String input(Long id, Model model){

        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions", permissions);

        //  判断id是否存在
        if (id != null) {
            //  调用Service层的selectById方法
            Role role = roleService.selectById(id);
            //  将获取到的数据设置到模型中
            model.addAttribute("role", role);
        }
        //  找视图/WEB-INF/views/role/input.ftl
        return "role/input";
    }

    //  新增或修改页面方法
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "角色保存或更新", expression = "role:saveOrUpdate")
    public String saveOrUpdate(Role role, Long[] ids){
        //  判断id是否存在
        if (role.getId() != null) {
            //  若存在，调用Service层的updateById方法
            roleService.updateById(role, ids);
        } else {
            //  若不存在，调用Service层的insert方法
            roleService.insert(role, ids);
        }
        //  找视图，重定向到首页
        return "redirect:/role/list.do";
    }
}
